package com.cf.game;

import com.cf.game.service.pojo.UserInfo;
import com.cf.game.service.util.ObjectJsonUtil;
import com.cf.serialize.Message;
import lombok.extern.slf4j.Slf4j;

import javax.websocket.Session;
import java.io.IOException;

/**
 * cf-game-Player
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/3/27 10:47
 */
@Slf4j
public class Player {

    private UserInfo userInfo;

    private Session session;

    public Player(UserInfo userInfo, Session session) {
        this.userInfo = userInfo;
        this.session = session;
    }

    /**
     * 单用户接收消息
     *
     * @param message
     */
    public synchronized void unicast(Message message) {
        synchronized (this.session) {
            try {
                log.info("开始推送消息【{}】", this.session);
                session.getAsyncRemote().sendText(ObjectJsonUtil.toString(message));
            } catch (IOException e) {
                log.error("webSocket消息发送失败！【{}】", message, e);
            }
        }
    }


    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }
}
